前端那些事

vuePress-theme-reco chenpeng    2020 - 2021
前端那些事 前端那些事

Choose mode

  • dark
  • auto
  • light
首页
文章目录
  • Browser
  • CSS
  • ES6
  • JavaScript
  • Network
  • TypeScript
  • Vue
  • Vue3
  • Webpack
标签
时间轴
GitHub
author-avatar

chenpeng

85

Article

25

Tag

首页
文章目录
  • Browser
  • CSS
  • ES6
  • JavaScript
  • Network
  • TypeScript
  • Vue
  • Vue3
  • Webpack
标签
时间轴
GitHub
  • TypeScript

    • TypeScript基本概念
    • TypeScript的静态类型
    • TypeScript数组类型注解
    • TypeScript中interface与type的区别

TypeScript数组类型注解

vuePress-theme-reco chenpeng    2020 - 2021

TypeScript数组类型注解

chenpeng 2020-11-30 TS

# 1.基本类型注解

const numberArr: number[] = [1, 2, 3]

const stringArr: string[] = ['a', 'b', 'c']

const undefinedArr: undefined[] = [undefined, undefined]
1
2
3
4
5

# 2.联合类型注解

const arr: (number | boolean | string)[] = [1, '2', true]
1

# 3.对象数组类型注解

const Stus1: { name: string, age: number }[] = [
    {
        name: 'cy',
        age: 22
    },
    {
        name: 'cy',
        age: 22
    }
]
1
2
3
4
5
6
7
8
9
10

# 4.使用别名

type Stu = { name: string, age: number }
const Stus2: Stu[] = [
    {
        name: 'cy',
        age: 22
    },
    {
        name: 'cy',
        age: 22
    }
]
1
2
3
4
5
6
7
8
9
10
11